<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Index extends CI_Controller {
    private function is_weixin(){
        if(! isset($_SERVER['HTTP_USER_AGENT']) ){
            return false;
        }
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_agent, 'MicroMessenger') === false) {
            return false;
        }else{
            return true;
        }
    }
    private function openid(){
        define("WEIXIN_APPID", HAO_WEIXIN_APPID);
        define("WEIXIN_APPSECRET", HAO_WEIXIN_APPSECRET);
        define("WEIXIN_MCHID", HAO_WEIXIN_MCHID);
        define("WEIXIN_KEY", HAO_WEIXIN_KEY);
        define("WEIXIN_SSLCERT_PATH", HAO_WEIXIN_KEY);
        define("WEIXIN_SSLKEY_PATH", HAO_WEIXIN_KEY);
        $this->load->library('WxPayPubHelper/SDKRuntimeException.php');
        $this->load->library('WxPayPubHelper/WxPaypubconfig.php');
        $this->load->library('WxPayPubHelper/WxPayPubHelper.php');
        //使用jsapi接口
        $jsApi = new \JsApi_pub();
        $openid = "-1";
        //通过code获得openid
        define("JS_API_CALL_URL", HAO_REDICT_URL);
        //微信登录返回code
        $code = $this->input->get('code');
        if (!$code) {
            $referer = $this->input->get_request_header('Referer');
            $this->session->set_userdata('Referer',$referer);
            //触发微信返回code码
            $url = $jsApi->createOauthUrlForCode(JS_API_CALL_URL);
            Header("Location: $url");
        } else {
            $jsApi->setCode($code);
            $openid = $jsApi->getOpenId();
            if ($openid == false) {
                exit(" 请求失败 未获取 openid,");
            }
        }
        return $openid;
    }
	public function login()
	{
        $this->load->database();
        $this->load->library('session');
        if($this->is_weixin()){
            $openid = $this->openid();
            $this->load->model('user');
            $user = $this->user->by_openid($openid);
            $this->session->set_userdata('user_info',$user);
        }
        else{
            $this->session->set_userdata('user_info',["id"=>-1]);
        }
        $Referer = $this->session->userdata('Referer');
        if(empty($Referer)){
            $Referer = "/";
        }
        Header("Location: $Referer");
	}

    /**
     * 图片转化
     */
    public function image(){
        if($this->input->get('src')){
            t_(file_get_contents($this->input->get('src')));
        }
        $this->output->cache(60*24*7);
    }
    public function test(){
        $this->load->view('test');
    }


    function admin_login(){
        $this->load->library('session');
        $this->session->set_userdata('admin',NULL);
        $login = $this->input->post('login');
        $pwd = $this->input->post('pwd');
        if($login && $pwd){
            if(
                $login == "admin" && $pwd == "admin"
            ){
                $this->session->set_userdata('admin',[
                    'id'=>1,
                    'user_name'=>'admin'
                ]);
                return t_("<script>location.href='/admin' </script>");
            }else{
                t_("<script> alert('密码错误') </script>");
            }
        }
        $this->load->view('admin/admin_login');
    }
}
